package com.mjf.scala.chapter08

/**
 * 匹配对象（样例类）
 */
object Scala04_TestMatch {
  def main(args: Array[String]): Unit = {

    // 创建用户对象
    val user: User = User("lucy", 18)

    // 对对象进行模式匹配
    val res: String = user match {
      case User("lucy", 18) => "yes"  // 通过unapply方法依次对比属性
      case _ => "no"
    }
    println(res)

  }
}

class User(var name: String, var age: Int) {}

object User {
  // 根据对象的属性创建对象
  def apply(name: String, age: Int): User = new User(name, age)
  // 根据对象获取对象的属性
  def unapply(user: User): Option[(String, Int)] = {
    if(user == null) {
      return None
    } else {
      return Some(user.name, user.age)
    }
  }
}